from common.base import Base, By


class AddGoodsPage(Base):
    """
    后台新增商品
    """

    def g_management(self):
        """
        g_management:商品管理
        """
        # 定位商品管理，点击
        self.click((By.XPATH, '//*[@id="menu-ul"]/li[2]'))

    def add_goods(self):
        # 定位添加商品点击
        self.click((By.LINK_TEXT, '添加新商品'))

    def min_frame(self):
        # 切换填写商品信息frame
        frame = self.find_element((By.NAME, 'main-frame'))
        # 切换frame
        self.driver.switch_to.frame(frame)

    def goods_input(self, goods_name):
        """
        goods_input:商品名输入框
        """
        # 定位商品名称，输入数据
        self.find_element((By.NAME, 'goods_name')).send_keys(goods_name)

    def goods_classification(self, index):
        """
        goods_classification:商品分类
        """
        # 定位商品分类,选择分类
        self.select((By.NAME, 'cat_id'), index)

    def goods_price(self, price):
        # 定位本店售价，输入数据
        self.find_element((By.NAME, 'shop_price')).send_keys(price)

    def btn(self):
        # 定位确定按钮
        self.click((By.XPATH, '//*[@id="tabbody-div"]/form/div/input[2]'))

    def w_text(self):
        # 获取标签文本
        return self.get_element_text((By.XPATH, '//*[@id="compareForm"]/div/div/div[2]/p/a'))

    def change_parent_frame(self):
        """切换到上一级frame"""
        self.driver.switch_to.parent_frame()

    def clear(self, locator):
        # 清空输入框内容
        self.find_element(locator).clear()

    def new_goods(self):
        # 定位搜索后的商品然后点击
        self.click((By.XPATH, '//*[@id="compareForm"]/div/div/div[1]'))

    def text(self):
        # 获取商品名
        return self.get_element_text((By.CLASS_NAME, 'goods_style_name'))
